<?php

global $_GPC;
global $_W;
$shopid = check_right(3);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
load()->model('mc');
if ($operation == 'display') {
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_marketing') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by displayorder asc, id desc');
    $grouptemp = mc_groups();
} else {
    if ($operation == 'updateMove') {
        foreach ($_GPC['displayorder'] as $index => $row) {
            pdo_update('j_hmoney_marketing', array('displayorder' => $row), array('id' => $index));
        }
        exit(json_encode(array('success' => true)));
    } else {
        if ($operation == 'post') {
            $id = $_GPC['id'];
            if ($id) {
                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_marketing') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
            } else {
                $item = array('starttime' => strtotime(date('Y-m-d')), 'endtime' => strtotime(date('Y-m-d')));
            }
            $gamelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_lotterygame') . ' WHERE weid = \'' . $_W['uniacid'] . '\' order by id desc');
            $grouptemp = mc_groups();
        } else {
            if ($operation == 'postdata') {
                $id = intval($_GPC['id']);
                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $_GPC['title'], 'status' => intval($_GPC['status']), 'starttime' => strtotime($_GPC['gametime']['start'] . ' 00:00:00'), 'endtime' => strtotime($_GPC['gametime']['end'] . ' 23:59:59'), 'gameid' => intval($_GPC['gameid']), 'limitweek' => implode(',', $_GPC['limitweek']), 'limithour' => implode(',', $_GPC['limithour']), 'num' => intval($_GPC['num']), 'ftype' => $_GPC['ftype'], 'fee' => $_GPC['fee'], 'content' => $_GPC['content'][$_GPC['ftype']], 'memberlevel' => implode(',', $_GPC['memberlevel']), 'description' => $_GPC['description']);
                if ($id) {
                    unset($data['weid']);
                    unset($data['groupid']);
                    pdo_update('j_hmoney_marketing', $data, array('id' => $id));
                } else {
                    pdo_insert('j_hmoney_marketing', $data);
                }
                exit(json_encode(array('success' => true)));
            } else {
                if ($operation == 'goodmarket') {
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodmarket') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by status desc,id desc');
                } else {
                    if ($operation == 'goodmarketpost') {
                        $id = $_GPC['id'];
                        if ($id) {
                            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goodmarket') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                        } else {
                            $item = array('starttime' => strtotime(date('Y-m-d')), 'endtime' => strtotime(date('Y-m-d')));
                        }
                        $goodcatelist = pdo_fetchall('SELECT id,title FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by displayorder asc, id desc');
                    } else {
                        if ($operation == 'selectpcate') {
                            $cid = intval($_GPC['cid']);
                            $selectid = $_GPC['selectid'] ? $_GPC['selectid'] : 0;
                            $where = $cid ? ' and pcate=\'' . $cid . '\' and id not in (' . $selectid . ') ' : ' and id in (' . $selectid . ')';
                            $goodcatelist = pdo_fetchall('SELECT id,title,thumb,marketprice FROM ' . tablename('j_hmoney_goods') . ' WHERE weid =\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' ' . $where . ' order by displayorder asc, id desc');
                            exit(json_encode(array('success' => true, 'list' => $goodcatelist)));
                        } else {
                            if ($operation == 'postgoodata') {
                                $id = intval($_GPC['id']);
                                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $_GPC['title'], 'status' => intval($_GPC['status']), 'starttime' => strtotime($_GPC['gametime']['start'] . ' 00:00:00'), 'endtime' => strtotime($_GPC['gametime']['end'] . ' 23:59:59'), 'limitweek' => implode(',', $_GPC['limitweek']), 'limithour' => implode(',', $_GPC['limithour']), 'discount' => trim($_GPC['discount']), 'contentid' => trim($_GPC['contentid']), 'description' => $_GPC['description'], 'createtime' => TIMESTAMP);
                                if ($id) {
                                    unset($data['weid']);
                                    unset($data['groupid']);
                                    unset($data['createtime']);
                                    pdo_update('j_hmoney_goodmarket', $data, array('id' => $id));
                                } else {
                                    $result = pdo_insert('j_hmoney_goodmarket', $data);
                                }
                                exit(json_encode(array('success' => true)));
                            } else {
                                if ($operation == 'delete') {
                                    $id = intval($_GPC['id']);
                                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_marketing') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                                    if (!$item) {
                                        message('活动不存在');
                                    }
                                    pdo_delete('j_hmoney_marketing', array('id' => $item['id']));
                                    message('删除成功', $this->createWebUrl('market'), 'success');
                                } else {
                                    if ($operation == 'deletegood') {
                                        $id = intval($_GPC['id']);
                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goodmarket') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                                        if (!$item) {
                                            message('活动不存在');
                                        }
                                        pdo_delete('j_hmoney_goodmarket', array('id' => $item['id']));
                                        message('删除成功', $this->createWebUrl('market', array('op' => 'goodmarket')), 'success');
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/market');